//
//  Date+Addition.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import Foundation
extension Date {
    static func formatTime(inputTime: String) -> String? {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        
        if let date = formatter.date(from: inputTime) {
            formatter.dateFormat = "hh:mm a"
            return formatter.string(from: date)
        }
        
        return nil // 如果时间格式无效
    }
    func toString(format: String = "yyyy-MM-dd HH:mm:ss") -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self)
    }
    func getTodayDate() -> String {
        let calendar = Calendar.current
        let dayOfWeek = calendar.component(.weekday, from: self)
        let dayOfWeekString = calendar.weekdaySymbols[dayOfWeek - 1]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM"
        let dateString = dateFormatter.string(from: self)
        let result = "\(dateString)   \(dayOfWeekString)"
        return result
    }
    func isToday() -> Bool {
        let calendar = Calendar.current
        let today = calendar.startOfDay(for: Date())
        let otherDate = calendar.startOfDay(for: self)
        
        return calendar.isDate(today, inSameDayAs: otherDate)
    }
}
